﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AdmLaboratorios.Business;
using AdmLaboratorios.DTO;

namespace AdmLaboratorios
{
    public partial class frmIRConsulta : Form
    {
        public frmIRConsulta()
        {
            InitializeComponent();
        }

        private void frmIRConsulta_Load(object sender, EventArgs e)
        {
            FormsHelper.FillCombo(typeof(eInformeRecepcionEstado), cbEstado);
            cbEstado.SelectedIndex = 0;
        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbEstado_SelectedIndexChanged(object sender, EventArgs e)
        {
            ReloadOrders();
        }

        private void gv_SelectionChanged(object sender, EventArgs e)
        {
            ReloadItems();
        }

        private void txFiltro_TextChanged(object sender, EventArgs e)
        {
            ReloadOrders();
        }

        private void ReloadOrders()
        {

            gv.DataSource = null;
            gvItems.DataSource = null;

            gv.DataSource = InformesRecepcion.Consulta(txFiltro.Text, cbEstado.Text.Trim().ToUpper());
            lblOrdenes.Text = ((DataTable)gv.DataSource).Rows.Count.ToString();
        }

        private void ReloadItems()
        {
            if (gv.SelectedRows.Count == 1)
            {
                gvItems.DataSource = null;
                gvItems.DataSource = InformesRecepcion.GetItemsByIR((int)gv.SelectedRows[0].Cells["Id"].Value);
                lblItems.Text = ((List<InformeRecepcionItemDTO>)gvItems.DataSource).Count.ToString();
            }
        }

        private void btnAgregar_Click(object sender, EventArgs e)
        {
            FormsFactory.ShowForm<frmIRAlta>();
        }

        private void btnModificar_Click(object sender, EventArgs e)
        {
            if (gv.SelectedRows.Count == 1)
            {
                var f = FormsFactory.ShowForm<frmIRModificacion>();
                f.SetValues((int)gv.SelectedRows[0].Cells["Id"].Value);
            }
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {

        }

        private void gv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (gv.SelectedRows.Count == 1)
            {
                var f = FormsFactory.ShowForm<frmIRModificacion>();
                f.SetValues((int)gv.SelectedRows[0].Cells["Id"].Value);
            }
        }

        private void btnClonar_Click(object sender, EventArgs e)
        {
            try
            {
                if (gv.SelectedRows.Count == 1)
                {
                    if (MessageBox.Show(
                        "Se realizará una copia idéntica del IR seleccionado. ¿Desea continuar?",
                        "Clonar Informe de Recepción",
                        MessageBoxButtons.OKCancel,
                        MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK)
                    {
                        IR ir = InformesRecepcion.Clonar((int)gv.SelectedRows[0].Cells["Id"].Value);
                        //OP op = OrdenesProduccion.Clonar((int)gv.SelectedRows[0].Cells["Id"].Value);

                        //Muestro el IR creado.
                        var f = FormsFactory.ShowForm<frmIRModificacion>();
                        f.SetValues(ir.InformeRecepcion.Id);
                    }
                }
            }
            catch (Exception ex)
            {
                FormsHelper.MsgErrorInesperado(ex);
            }
        }

        private void frmIRConsulta_Shown(object sender, EventArgs e)
        {
            txFiltro.Focus();
        }


    }
}
